package com.own.component.util.currency.model;

import lombok.Getter;

import java.io.Serializable;

/**
 * Renminbi
 *
 * @author chenxueli
 * @date 2023-07-06 16:13:00
 */
@Getter
public class Renminbi implements Serializable, Comparable<Renminbi> {

    /**
     * 数值（分）
     */
    private final Integer value;

    public Renminbi(int value) {
        if (value < 0) {
            throw new IllegalArgumentException("人民币的金额不能小于0");
        }
        this.value = value;
    }

    public static void main(String[] args) {
        System.out.println("new Renminbi(0) = " + new Renminbi(0));
        System.out.println("new Renminbi(1) = " + new Renminbi(1));
        System.out.println("new Renminbi(12345) = " + new Renminbi(12345));
    }

    @Override
    public String toString() {
        // 获取整数部分
        var a = value / 100;
        // 获取小数部分
        var b = value % 100;
        if (b == 0) {
            return "¥" + a + ".00";
        }
        if (b < 10) {
            return "¥" + a + ".0" + b;
        }
        return "¥" + a + "." + b;
    }

    @Override
    public int compareTo(Renminbi o) {
        return this.value.compareTo(o.getValue());
    }

}
